package FlowGraph.Nodes;

import antlr.collections.AST;

import java.util.*;

import util.Line;
import FlowGraph.*;
import FlowGraph.EdgeInfo.*;
import FlowGraph.StatementInfo.StatementInfo;

//////////////////////////////////////////////////////////////////////////
//// IfBeginNode
/**

@author Ivan Jeukens
@version v 0.1 03/10/2001
*/
public class IfBeginNode extends FlowGraphNode {

    public IfBeginNode(AST node) {
        super(node, IF_BEGIN, 1, 2); 
        _condition = node.getFirstChild();

        _conditionInfo = new StatementInfo(this, _condition);
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    public FlowGraphEdge getThenEdge() {
        Iterator ite = _outputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isThen()) {
                return edge;
            }
        }
        return null;
    }
    
    public FlowGraphNode getThenNode() {
        FlowGraphEdge edge = getThenEdge();
        if(edge != null) return edge.getTo();
        
        return null;
    }
    
    public FlowGraphEdge getElseEdge() {
        Iterator ite = _outputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            IfBranchEdge einfo = (IfBranchEdge) edge.getEdgeInfo();
            if(einfo.isElse()) {
                return edge;
            }
        }
        return null;
    }

    public FlowGraphNode getElseNode() {
        FlowGraphEdge edge = getElseEdge();
        if(edge != null) return edge.getTo();

        return null;
    }

    public String toString() {
        return new String("if(" +  ")");
    }
    
    public void setIfEnd(IfEndNode node) {
        _ifend = node;
    }
    
    public IfEndNode getIfEnd() {
        return _ifend;   
    }

    public AST getCondition() {
        return _condition;
    }

    public void setConditionLine(Line l) {
        _conditionLine = l;
    }
    
    public Line getConditionLine() {
        return _conditionLine;
    }
    
    public void setThenEndLine(Line l) {
        _thenEndLine = l;
    }
    
    public Line getThenEndLine() {
        return _thenEndLine;
    }
    
    public void setElseEndLine(Line l) {
        _elseEndLine = l;
    }

    public Line getElseEndLine() {
        return _elseEndLine;
    }

    public void setElseBegLine(Line l) {
        _elseBegLine = l;
    }

    public Line getElseBegLine() {
        return _elseBegLine;
    }

    public StatementInfo getStatementInfo() {
        return _conditionInfo;
    }
    
    public void setBreakScopeThen(FlowGraphNode node) {
        _breakScopeThen = node;
    }
    
    public void setBreakScopeElse(FlowGraphNode node) {
        _breakScopeElse = node;
    }
    
    public FlowGraphNode getBreakScopeThen() {
        return _breakScopeThen;
    }
    
    public FlowGraphNode getBreakScopeElse() {
        return _breakScopeElse;
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private AST _condition;

    private IfEndNode _ifend;
    private Line _conditionLine;
    private Line _thenEndLine;
    private Line _elseBegLine;
    private Line _elseEndLine;

    // Aponta para o ultimo no do corpo do branch antes de quebrar o escopo
    private FlowGraphNode _breakScopeThen;
    private FlowGraphNode _breakScopeElse;
    
    private StatementInfo _conditionInfo;
}
